﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace COFE.Core.Filter
{
    [EntryFilter("filetype")]
    public class FileTypeEntryFilter : EntryFilterBase
    {
        public const string File = "file";
        public const string Folder = "folder";

        public FileTypeEntryFilter(string type, string id = "filetype")
            : base(type.TrimStart('.').Replace("?","").Replace("*","")
            , id)
        {
            SupportedMatchType = MatchType.MatchThisEntry;
        }

        public override MatchType matches(IInternalFileSystemInfoExA entry,
            params IOptionFilter[] options)
        {                        
            bool match = false;

            if (entry is IDirectoryInfo)
                match = Value == FileTypeEntryFilter.Folder;
            else
            {
            string ext = COFEPath.GetExtension(entry.Name).TrimStart('.');
            match = Value.Equals(ext, 
                StringComparison.InvariantCultureIgnoreCase) ||
                Value.Equals(File, StringComparison.InvariantCultureIgnoreCase);            
            }

            return match ? MatchType.MatchThisEntry : MatchType.None;
        }            
    }
}
